const request=require("superagent");
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const util = require('util');
const charset = require("superagent-charset")(request);
const fs=require('fs');
var mysql      = require('mysql');
const assert = require('assert');
var connection = mysql.createConnection({
  host     : '127.0.0.1',
  user     : 'root',
  password : '123456',
  database : 'matter'
});
connection.query = util.promisify(connection.query);
async function getResourceTree(page) {
    var resource = await page._client.send('Page.getResourceTree');
    return resource.frameTree;
}

async function getResourceContent(page, url) {
    const { content, base64Encoded } = await page._client.send(
        'Page.getResourceContent',
        { frameId: String(page.mainFrame()._id), url },
    );
    assert.equal(base64Encoded, true);
    return content;
};
const Agent=[
 	"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
    "Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    "MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
    "JUC (Linux; U; 2.3.7; zh-cn; MB200; 320*480) UCWEB7.9.3.103/139/999",
    "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0a1) Gecko/20110623 Firefox/7.0a1 Fennec/7.0a1",
    "Opera/9.80 (Android 2.3.4; Linux; Opera Mobi/build-1107180945; U; en-GB) Presto/2.8.149 Version/11.10",
    "Mozilla/5.0 (Linux; U; Android 3.0; en-us; Xoom Build/HRI39) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13",
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/1A542a Safari/419.3",
    "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7",
    "Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10",
    "Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, like Gecko) Version/6.0.0.337 Mobile Safari/534.1+",
    "Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0",
    "Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/20.0.019; Profile/MIDP-2.1 Configuration/CLDC-1.1) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.18124",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; HTC; Titan)",
    "Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36",
];
const timeout={
	response:50000,
	deadline:600000,
}
Date.prototype.format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "h+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}
function delay(t){
    return new Promise(resolve => setTimeout(resolve, t))
}

function rnd(n, m){
    var random = Math.floor(Math.random()*(m-n+1)+n);
    return random;
}
function getRndIP(){
    var ip=[];
    for(var i=0;i<4;i++){
    ip.push(rnd(10,250));
    }
    return ip.join(".");
}
//封装请求 实现失败重试
async function requestWithRetry(url, retryCount = 3, timeout = 5000,option) {
  return new Promise((resolve, reject) => {
    let numRetries = 0;
 
    const makeRequest = () => {
      request
        .get(url)
        .set(option)
        .timeout(timeout) // 设置超时时间
        .then(response => {
          resolve(response);
        })
        .catch(error => {
          if (error.code === 'ECONNABORTED' && numRetries < retryCount) {
            numRetries += 1;
            console.log(`Request timed out. Retrying... (Attempt ${numRetries})`);
            makeRequest(); // 重试请求
          } else {
            reject(error);
          }
        });
    };
 
    makeRequest();
  });
}
async function getList(){
	
    //通过日志恢复数据
	// let arr = [3229,3302,3711,3712,3736,3830,4026,4028,4041,4042,4043,4121,4183,4561,4639,4990];
	// for (var q = 0; q < arr.length; q++) {

	// 	let sql=`select * from cms_admin_log where param_id = ${arr[q]} and uid = 6 and controller = 'matter' ORDER by id desc`;
	// 	let result=await connection.query(sql);
	// 	console.log(result[0].param_id);
	// 	console.log(result[0].uid);
	// 	let points = JSON.parse(result[0]['param']).points;
	// 	if(points){
	// 		for (var i = 0; i < points.content.length; i++) {
				



	// 			// await connection.query(`update m_matter_a SET full_name = '${implement_subject}' where id = '${result[i]['id']}'`);

	// 			console.log(points.content[i])
	// 			addDb({
	// 				'uid':result[0].uid,
	// 				'content':points.content[i],
	// 				'matter_id':result[0].param_id
	// 			})
				
	// 		}
	// 	}else{
	// 		console.log(JSON.parse(result[0]['param']))
	// 	}
		
	// }

	//补充事项表
    let sql=`select * from m_matter where id > 11954 AND arrive_num IS NULL`;
    let result=await connection.query(sql);
    for (var i = 0; i < result.length; i++) {
        var ip=getRndIP();
        var agent=Agent[rnd(0,Agent.length-1)];
        agent=Agent[0];
        var option={
            "Host":"zwfw.nmg.gov.cn",
            "User-Agent":agent,
            "Referer":'https://zwfw.nmg.gov.cn/',
            "X-Forwarded-For":ip,
        }
        //事项详情页
        let url=`https://zwfw.nmg.gov.cn/handle_affairs_detail?item_detail_id=${result[i]['ItemID']}&regionCode=150000&highlight=&record=istrue`;
        console.log(result[i]['id'],result[i]['name'],url);
        await delay(500);
        let res = await requestWithRetry(url, 3, 5000,option).then(response => {
            // console.log('Response received:', response);
            return response;
          }).catch(error => {
            console.error('Error:', error);
          });
        // let res=await request.get(url).set(option).timeout(timeout).buffer(true);
        let body=res.text;
        var $ = cheerio.load(body);
        //中介服务
        let intermediary_services = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq(1) td:eq(1)').text().trim();
        let treq = 0
        if(intermediary_services == '是'){
            intermediary_services_name = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq(2) td:eq(0)').text().trim();
            treq = 1;
        }
        let computer_link = null;
        if($('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(4+treq)+') td:eq(1) p').length>0 && $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(4+treq)+') th:eq(1)').text() == '计算机端在线办理跳转地址'){
            let str1 = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(4+treq)+') td:eq(1) p').attr('onclick');
            computer_link = str1.substring(str1.indexOf("('")+2,str1.indexOf("',"));
        }
        let phone_link = null;
        if($('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(5+treq)+') td:eq(1) p').length>0 && $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(5+treq)+') th:eq(1)').text() == '移动端办理地址'){
            let str2 = $('.vue-banshi-jian-x-jiben-x tbody:eq(1) tr:eq('+(5+treq)+') td:eq(1) p').attr('onclick');
            phone_link = str2.substring(str2.indexOf("('")+2,str2.indexOf("',"));
        }
        let online_office_depth = null;
        if(result[i]['online_office_depth']){
            online_office_depth = result[i]['online_office_depth'].substring(0,result[i]['online_office_depth'].indexOf('）')+1);
        }
        //实施主体
        let data = {
            'arrive_num':$('.vue-banshi-bot-s-l:eq(0) em:eq(0)').text().trim(),
            'legal_time':$('.vue-banshi-bot-s-l:eq(0) em:eq(1)').text().trim(),
            'promise_time':$('.vue-banshi-bot-s-l:eq(0) em:eq(2)').text().trim(),
            'online_office_depth':online_office_depth,
            'computer_link':computer_link,
            'phone_link':phone_link,
        };
        console.log(data)
        await addDb(data,result[i]['id']);
    }



		
}


//插入数据库
async function addDb(data,id){
    // console.log(data.nameId)
    //插入数据
        
    let sql=`select * from m_matter WHERE id='${id}' AND arrive_num IS NOT NULL`;
    let result=await connection.query(sql);
    if(result.length>0){
     console.log(`add ${id} error`);
    }else{
        let addSql=`update m_matter SET ? where id = '${id}'`;
        let addSqlParams = data;
        return await new Promise(resolve => {
            connection.query(addSql,addSqlParams,function (err, result) {
                if(err){
                 console.log('[INSERT ERROR] - ',err.message);
                 return;
                }else{
                    console.log('UPDATA ID:',id);
                    return resolve(result.insertId);
                }        
                
            });
        })
        
    }
    
        
}

//插入数据库
async function asySql(addSql,addSqlParams){
    return await new Promise(resolve => {
        connection.query(addSql,addSqlParams,function (err, result) {
            if(err){
             console.log('[INSERT ERROR] - ',err.message);
             return;
            }else{
                console.log('INSERT ID:',result.insertId);
                return resolve(result.insertId);
            }        
            
        });
    })
        
}
getList();
